<?php

namespace App\Providers;

use App\Http\Middleware\LogRequest;
use App\Models\Files;
use App\Models\Participant;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{

    /**
     * All of the container singletons that should be registered.
     *
     * @var array
     */
    public $singletons = [
        //基础服务
        'link' => 'App\Services\Link',
        'roleService' => 'App\Services\System\RoleService',
        'logService' => 'App\Services\System\LogService',
        'scaleService' => 'App\Services\System\ScaleService',
        'userService' => 'App\Services\System\UserService',
        'groupService' => 'App\Services\System\GroupService',
        'IndexService' => 'App\Services\System\IndexService',
        'loginService' => 'App\Services\System\LoginService',
        'termService' => 'App\Services\System\TermService',
        'definedGroupService' => 'App\Services\System\DefinedGroupService',
        'dicService' => 'App\Services\System\DicService', //字典
        'teacherService' => 'App\Services\System\TeacherService',
        'activityService' => 'App\Services\System\ActivityService',
        'filesService' => 'App\Services\System\FilesService',
        //获奖情况
        'awardsService' => 'App\Services\Awards\AwardsService',
        'workService' => 'App\Services\Work\WorkService', //带教情况
        'inWorkService' => 'App\Services\Work\InWorkService', //校内工作
        'outWorkService' => 'App\Services\Work\OutWorkService', //校外工作
        'teachingSubjectService' => 'App\Services\Teaching\TeachingSubjectService',
        'teachingExportService' => 'App\Services\Teaching\TeachingExportService',//教育教学导出
        'liveService' => 'App\Services\Teaching\LiveService', //直播管理
        'tactivityService' => 'App\Services\Teaching\ActivityService', //教研活动
        'headMasterService' => 'App\Services\Education\HeadMasterService',
        'educationExportService' => 'App\Services\Education\EducationExportService',//实践活动导出
        'creditService' => 'App\Services\Faculty\CreditService',
        'platformCreditService' => 'App\Services\Faculty\PlatformCreditService',
        'facultyExportService' => 'App\Services\Faculty\FacultyExportService',//教师培训导出
        'archivesService' => 'App\Services\Information\ArchivesService',
        'downldService' => 'App\Services\Information\DownldService',
        'jobService' => 'App\Services\Rating\JobService', //岗位竞聘
        'checkService' => 'App\Services\Rating\CheckService', //学年考核
        'taskService' => 'App\Services\System\TaskService', //待办事项
        'planService' => 'App\Services\Education\PlanService',//计划和总结

        //基础仓库
        'roleRepository' => 'App\Repositories\System\RoleRepository',
        'logRepository' => 'App\Repositories\System\LogRepository',
        'scaleRepository' => 'App\Repositories\System\ScaleRepository',
        'groupRepository' => 'App\Repositories\System\GroupRepository',
        'userRepository' => 'App\Repositories\System\UserRepository',
        'IndexRepository' => 'App\Repositories\System\IndexRepository',
        'dicRepository' => 'App\Repositories\System\DicRepository', //字典
        'dicitemRepository' => 'App\Repositories\System\DicItemRepository', //字典
        'dictionaryItemRepository' => 'App\Repositories\System\DictionaryItemRepository', //老字典
        'teacherRepository' => 'App\Repositories\System\TeacherRepository',
        'activityRepository' => 'App\Repositories\System\ActivityRepository',
        'participantRepository' => 'App\Repositories\System\ParticipantRepository',
        'systemTaskRepository' => 'App\Repositories\System\SystemTaskRepository',
        'teachingSubjectRepository' => 'App\Repositories\Teaching\TeachingSubjectRepository',
        'teachingActivityRepository' => 'App\Repositories\Teaching\TeachingActivityRepository',
        'teachingHomeworkRepository' => 'App\Repositories\Teaching\TeachingHomeworkRepository',
        'liveRepository' => 'App\Repositories\Teaching\LiveRepository',//直播管理
        'headMasterRepository' => 'App\Repositories\Education\HeadMasterRepository',
        'creditRepository' => 'App\Repositories\Faculty\CreditRepository',

    ];

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton(LogRequest::class);
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(UrlGenerator $url)
    {
        $protocol = env('APP_PROTOCOL', 'http');
        if($protocol == 'https'){
            //HTTPS
            $url->forceScheme('https'); //这里用https，没有的话自己添加下
        }
        Relation::morphMap([
            Files::ILK_GET_AWARDS => 'App\Models\ActivityInfo',
            Files::ILK_PART => 'App\Models\Participant',
        ]);


        // 若开启 debug 模式，将查询 SQL 打印到日志文件中。
        if (config('app.debug')) {
            DB::listen(
                function ($sql) {
                    foreach ($sql->bindings as $i => $binding) {
                        if ($binding instanceof \DateTime) {
                            $sql->bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
                        } else {
                            if (is_string($binding)) {
                                $sql->bindings[$i] = "'$binding'";
                            }
                        }
                    }

                    // Insert bindings into query
                    $query = str_replace(['%', '?'], ['%%', '%s'], $sql->sql);
                    $query = vsprintf($query, $sql->bindings);

                    // Save the query to file
                    Log::channel('query')->info($query);
                }
            );
        }
    }
}
